package LOGIN;

import SelectAdmin.impl.AdminSelectImpl;
import entity.Admin;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/Login", initParams={
        @WebInitParam(name = "count", value = "3")
})
public class Login extends HttpServlet {
    Integer count;
    @Override
    public void init(ServletConfig config) throws ServletException {

        //拿到初始次数
        count = Integer.parseInt(config.getInitParameter("count"));

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

//        判断用户和密码是否为空
        if ((username==null || username =="")||(password==null||password=="")){
            req.setAttribute("errorMessage","用户或密码不能为空");
            req.getRequestDispatcher("/error(null).jsp").forward(req,resp);
            return;
        }

        AdminSelectImpl AdminUser = new AdminSelectImpl();

//        数据传入，并返回正确的用户登录信息  //而你这里在使用人家方法的时候，只传了一个值，懂了吗
        Admin admin = AdminUser.selectAdmin(username,password);

        String adminAccount = admin.getAccount();
        String adminPassword = admin.getPassword();
//        判断返回的用户名是否为null
        if (adminAccount==null){
            req.setAttribute("errorMessage","您输入的账户:"+username+"不存在");
            req.getRequestDispatcher("/error(null).jsp").forward(req,resp);
            return;
        }
        ServletContext application = req.getServletContext();
        Integer errorCount =(Integer) application.getAttribute("errorCount" + username);

//      判断密码错误次数
        if (errorCount!=null && errorCount==0){
            req.setAttribute("errorMessage","登录次数用完了");
            req.getRequestDispatcher("/error(null).jsp").forward(req,resp);
            return;
        }

//        判断密码
        if (!adminPassword.equals(adminPassword)){
            if (errorCount==null){
                Integer i = count;
                //如果为null,就是第一次
                application.setAttribute("errorCount"+ username,--i);
            }else {
                //否则就是2、3、。。。。n次
                application.setAttribute("errorCount"+ username,--errorCount);
            }
//        密码错错误,并提醒用户
            req.setAttribute("errorMessage","密码错误！错误次数还有"+application.getAttribute("errorCount"+ username)+"次");
            req.getRequestDispatcher("/error(null).jsp").forward(req,resp);
            return;
        }
;
        //重置次数
        application.removeAttribute("errorCount"+ username);
        //把用户存入作用域里
        req.getSession().setAttribute("user",username);
        //登录成功便跳转到主页
        req.getRequestDispatcher("/stf").forward(req,resp);

    }
}